#include <stdio.h>
#include <time.h>   /*调用time函数*/
#include <stdlib.h> /*调用srand函数*/
int main()    /*主函数*/
{
    int magic,number,n;   /*定义变量*/
    char ch1,ch2;
    srand(time(NULL));   /*产生随机数并刷新*/
    while (1)
    {
        magic = rand() % 100 + 1;   /*产生一个1到100的随机数*/
        n = 0;   /*用于记录循环次数*/
        do    /*直到猜到数字或者猜错十次结束循环*/
        {
            printf("Please guess a magic number:");
            scanf("%d", &number);
            if (number >= 0 && number <= 100)
            {
                if (number == magic)
                {
                    printf("Right\n");
                    n++;   /*每猜一次次数就加一*/
                    break;
                }
                else if (number < magic)
                {
                    printf("Wrong!Too low\n");
                    n++;
                }
                else
                {
                    printf("Wrong!Too high\n");
                    n++;
                }
            }
            else
            {
                printf("Input error\n");
                n++;
            }
        } while (number != magic && n < 10);
        printf("You guessed %d times\n", n);   /*显示猜的次数*/
        printf("Do you want to play again:");
        scanf(" %c", &ch1);
        
        if (ch1 == 'y' || ch1 == 'Y')
        {
            continue;   /*不执行下面的语句重新开始循环*/
        }
        if (ch1 == 'n' || ch1 == 'N')
        {
            break;     /*跳出循环不再执行循环语句*/
        }
        else
        {
            do    /*当输入的字符不规范时开始循环直到输入规范的字符*/
            {
                printf("Input error\n");
                printf("Do you want to play again:");
                scanf(" %c",&ch2);
            } while (ch2 != 'y'&&ch2 != 'Y'&&ch2 !='n'&&ch2 != 'N');
            if(ch2 == 'y'||ch2 == 'Y')
            {
                continue;  /*重新开始循环*/
            }
            if(ch2 == 'n'||ch2 == 'N')
            {
                break;     /*跳出循环不在执行循环语句*/
            }
        }
        
    }
    return 0;
}